package com.example.demo.service;

import com.example.demo.dao.BookInfoMapper;
import com.example.demo.dao.CommentMapper;
import com.example.demo.dao.CustomMapper;
import com.example.demo.entity.BookInfo;
import com.example.demo.entity.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private CustomMapper customMapper;

    @Autowired
    private BookInfoMapper bookInfoMapper;

    @Override
    public void insertComment(Comment comment) {

        comment.setDate(new Date());
        comment.setFlag(0);
        commentMapper.insert(comment);
    }

    @Override
    public List<Comment> selectCommentByStoreId(int storeId) {

        List<Comment> commentList= commentMapper.selectAllComments();
        for(Comment comment:commentList){
            BookInfo bookInfo=bookInfoMapper.selectByPrimaryKey(new Integer(comment.getBook_id()));
            comment.setBookTitle(bookInfo.getName());
        }

        return commentList;
    }


    @Override
    public List<Comment> selectCommentByBookId(int bookId) {

        Example example=new Example(Comment.class);
        Example.Criteria criteria=example.createCriteria();

        criteria.andEqualTo("book_id",bookId);
        criteria.andEqualTo("flag",1);
        return commentMapper.selectByExample(example);
    }

    @Override
    public int deleteByCommentId(int commentId) {
        return commentMapper.deleteByPrimaryKey(new Integer(commentId));
    }

    @Override
    public Comment selectByCommentId(int commentId) {

        Comment comment=commentMapper.selectByPrimaryKey(new Integer(commentId));


        BookInfo bookInfo=bookInfoMapper.selectByPrimaryKey(new Integer(comment.getBook_id()));

        comment.setBookTitle(bookInfo.getName());
        return comment;
    }


    @Override
    public int updateComment(Comment comment) {
        return commentMapper.updateStatus(comment.getComment_id(),comment.getFlag());
    }
}
